package com.pxx.pj.pxx.controller;


import com.pxx.pj.common.pojo.JsonResult;
import com.pxx.pj.common.pojo.PageObject;
import com.pxx.pj.pxx.pojo.PxxUser;
import com.pxx.pj.pxx.pojo.PxxUserDept;
import com.pxx.pj.pxx.service.PxxUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
public class PxxUserController {
    @Autowired
    private PxxUserService pxxUserService;



    @RequestMapping("/user/doUpdatePassword")
    public JsonResult doUpdatePassword(String newPwd,String cfgPwd,String pwd)
    {
        pxxUserService.updatePassword(newPwd, cfgPwd, pwd);
        return new JsonResult("update OK");
    }
    @RequestMapping("/user/doLogin")
    public JsonResult doLogin(String username,String password,boolean isRememberMe){
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token=new UsernamePasswordToken(username,password);
        if(isRememberMe) {
            token.setRememberMe(true);
        }
        token.setRememberMe(isRememberMe);
        subject.login(token);
        return new JsonResult("登录成功");
    }

    @RequestMapping("user/doUpdateObject")
    public JsonResult doUpdateObject(
            PxxUser entity, Integer[] roleIds){
        pxxUserService.updateObject(entity,roleIds);
        return new JsonResult("update ok");
    }


    @RequestMapping("user/doFindObjectById")
    public JsonResult doFindObjectById(
            Integer id){
        Map<String,Object> map=
                pxxUserService.findObjectById(id);
        return new JsonResult(map);
    }



    @RequestMapping("user/doFindPageObjects")
    public JsonResult doFindPageObjects(String username,Integer pageCurrent){
        return  new JsonResult(pxxUserService.findPageObjects(username, pageCurrent));
    }
    @RequestMapping("user/doValidById")
    public JsonResult doValidById(Integer id,Integer valid){
        pxxUserService.validById(id, valid);
        return new JsonResult("update ok");
    }

    @RequestMapping("user/doSaveObject")
    public JsonResult doSaveObject(
            PxxUser entity,
            Integer[] roleIds){
        pxxUserService.saveObject(entity,roleIds);
        return new JsonResult("save ok");
    }

}
